//
//  LYDrawer.m
//  TestDrawer_--001
//
//  Created by 刘杨 on 15/9/8.
//  Copyright (c) 2015年 刘杨. All rights reserved.
//

#import "LYDrawer.h"

@interface LYDrawer ()
{
    //用来判断  是左抽屉 还是右抽屉
    BOOL _isLeft;
}

//私有属性
//用来记录抽屉是否显示
@property (nonatomic, assign) BOOL isShow;
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UIImageView *rightImageView;

@end

@implementation LYDrawer

- (instancetype)initWithMainViewController:(UIViewController *)mainViewController leftViewController:(UIViewController *)leftViewController rightViewController:(UIViewController *)rightViewController{
    if (self = [super init]) {
        _leftViewController = leftViewController;
        _rightViewController = rightViewController;
        _mainViewController = mainViewController;
        //初始化属性
        _isSwipeGesture = YES;
        _animationDuration = 0.38;
        _drawerWidth = 180;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addAllChildViewController];
    [self addGestuerRecognizer];
}

#pragma mark - 
#pragma mark - 私有方法
//添加控制器
- (void)addAllChildViewController{
    //先添加左右视图，并将其隐藏起来
    if (_leftViewController) {
        //为优化性能，如果没有图片，则不创建imageView
        if (_leftBackgroudImage) {
            _leftImageView = [[UIImageView alloc] initWithFrame:_leftViewController.view.bounds];
            _leftImageView.image = self.leftBackgroudImage;
            _leftImageView.contentMode = UIViewContentModeScaleAspectFill;
            _leftImageView.userInteractionEnabled = YES;
            [_leftViewController.view addSubview:_leftImageView];
            _leftImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        }
        _leftViewController.view.frame = self.view.bounds;
        [self addChildViewController:_leftViewController];
        [self.view addSubview:_leftViewController.view];
        _leftViewController.view.hidden = YES;
        _leftViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }
    
    if (_rightViewController) {
        //同上
        if (_rightBackgroudImage) {
            _rightImageView = [[UIImageView alloc] initWithFrame:_rightViewController.view.bounds];
            _rightImageView.contentMode = UIViewContentModeScaleAspectFill;
            _rightImageView.userInteractionEnabled = YES;
            _rightImageView.image = self.rightBackgroudImage;
            [_rightViewController.view addSubview:_rightImageView];
            _rightImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        }
        _rightViewController.view.frame = self.view.bounds;
        [self addChildViewController:_rightViewController];
        [self.view addSubview:_rightViewController.view];
        _rightViewController.view.hidden = YES;
        _rightViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    }
    
    //最后加主控制器视图
    if (_mainViewController) {
        _mainViewController.view.frame = self.view.bounds;
        [self addChildViewController:_mainViewController];
        [self.view addSubview:_mainViewController.view];
        _mainViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }
}

//添加手势方法
- (void)addGestuerRecognizer{
    //轻拍手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRAction:)];
    //如果mianViewController 没有被创建，那么给self.view加，防止崩溃
    if (_mainViewController) {
        [_mainViewController.view addGestureRecognizer:tapGR];
    }else{
        [self.view addGestureRecognizer:tapGR];
    }
    
    //平移手势
    if (self.isSwipeGesture) {
        //如果是否要手势的属性是yes的话，那么加左右抽屉手势
        UISwipeGestureRecognizer *leftSwipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeGRAction:)];
        [self.view addGestureRecognizer:leftSwipeGR];
        
        UISwipeGestureRecognizer *rightSwipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeGRAction:)];
        rightSwipeGR.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.view addGestureRecognizer:rightSwipeGR];
    }
}

//实现轻拍手势-->为了将视图控制器移回
- (void)tapGRAction:(UITapGestureRecognizer *)sender{
    if (_mainViewController && _isShow) NSLog(@"移回");
    [self hideDrawer];
    _isShow = NO;
    
}

//实现平移手势方法-->为了将左右抽屉展现
- (void)leftSwipeGRAction:(UISwipeGestureRecognizer *)sender{
    if (_isShow) return;
    _isLeft = YES;
    if (_leftViewController) NSLog(@"左抽屉出来");
    [self showDrawer];
    _isShow = YES;
}

- (void)rightSwipeGRAction:(UISwipeGestureRecognizer *)sender{
    if (_isShow) return;
    _isLeft = NO;
    if (_rightViewController) NSLog(@"右抽屉出来");
    [self showDrawer];
    _isShow = YES;
}

//抽屉出现
- (void)showDrawer{
    [UIView animateWithDuration:self.animationDuration animations:^{
        (_isLeft ? _leftViewController : _rightViewController).view.hidden = NO;
        _mainViewController.view.transform = CGAffineTransformTranslate(_mainViewController.view.transform, (_isLeft ? self.drawerWidth : -self.drawerWidth), 0);
        _mainViewController.view.transform = CGAffineTransformScale(_mainViewController.view.transform, 0.8, 0.8);
    }];
}

//抽屉关闭
- (void)hideDrawer{
    [UIView animateWithDuration:self.animationDuration animations:^{
        _mainViewController.view.transform = CGAffineTransformTranslate(_mainViewController.view.transform, (_isLeft ? -self.drawerWidth : self.drawerWidth), 0);
        _mainViewController.view.transform = CGAffineTransformMakeScale(1, 1);

        //在这重新设置一下main.view
        _mainViewController.view.frame = self.view.bounds;
    }completion:^(BOOL finished) {
        (_isLeft ? _leftViewController : _rightViewController).view.hidden = YES;
    }];
}

#pragma mark - 公共方法
#pragma mark - 推出左右抽屉
- (void)presentLeftDrawerController{
    _isShow ? [self tapGRAction:nil] : [self leftSwipeGRAction:nil];
}

- (void)presentRightDrawerController{
    _isShow ? [self tapGRAction:nil] : [self rightSwipeGRAction:nil];
}

#pragma mark -
#pragma mark - 触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded] && self.view.window == nil) {
        self.view = nil;
    }
}
@end
